package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.system.domain.YyAppointmentTime;
import com.ruoyi.system.domain.YyHospitalDepartment;
import com.ruoyi.system.mapper.YyAppointmentTimeMapper;
import com.ruoyi.system.mapper.YyHospitalDepartmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.YyAppointmentMapper;
import com.ruoyi.system.domain.YyAppointment;
import com.ruoyi.system.service.IYyAppointmentService;
import com.ruoyi.common.core.text.Convert;

/**
 * 预约Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-24
 */
@Service
public class YyAppointmentServiceImpl implements IYyAppointmentService
{
    @Autowired
    private YyAppointmentMapper yyAppointmentMapper;
    @Autowired
    private YyAppointmentTimeMapper yyAppointmentTimeMapper;
    @Autowired
    private YyHospitalDepartmentMapper yyHospitalDepartmentMapper;

    /**
     * 查询预约
     *
     * @param appointmentId 预约主键
     * @return 预约
     */
    @Override
    public YyAppointment selectYyAppointmentByAppointmentId(Long appointmentId)
    {
        return yyAppointmentMapper.selectYyAppointmentByAppointmentId(appointmentId);
    }

    /**
     * 查询预约列表
     *
     * @param yyAppointment 预约
     * @return 预约
     */
    @Override
    public List<YyAppointment> selectYyAppointmentList(YyAppointment yyAppointment)
    {
        return yyAppointmentMapper.selectYyAppointmentList(yyAppointment);
    }

    /**
     * 新增预约
     *
     * @param yyAppointment 预约
     * @return 结果
     */
    @Override
    public int insertYyAppointment(YyAppointment yyAppointment)
    {
        return yyAppointmentMapper.insertYyAppointment(yyAppointment);
    }

    /**
     * 修改预约
     *
     * @param yyAppointment 预约
     * @return 结果
     */
    @Override
    public int updateYyAppointment(YyAppointment yyAppointment)
    {
        return yyAppointmentMapper.updateYyAppointment(yyAppointment);
    }

    /**
     * 批量删除预约
     *
     * @param appointmentIds 需要删除的预约主键
     * @return 结果
     */
    @Override
    public int deleteYyAppointmentByAppointmentIds(String appointmentIds)
    {
        return yyAppointmentMapper.deleteYyAppointmentByAppointmentIds(Convert.toStrArray(appointmentIds));
    }

    /**
     * 删除预约信息
     *
     * @param appointmentId 预约主键
     * @return 结果
     */
    @Override
    public int deleteYyAppointmentByAppointmentId(Long appointmentId)
    {
        return yyAppointmentMapper.deleteYyAppointmentByAppointmentId(appointmentId);
    }

    @Override
    public int insertYyAppointment2(YyAppointment yyAppointment) {
        String yueTime = yyAppointment.getYueTime();
        Integer departmentId1 = yyAppointment.getDepartmentId();
        YyAppointmentTime yyAppointmentTime =yyAppointmentTimeMapper.selectYyAppointmentTime(yueTime,departmentId1);
        if (yyAppointmentTime.getStatus() != null){
            throw new RuntimeException("此时间段已被预约");
        }
        Integer departmentId = yyAppointment.getDepartmentId();
        YyHospitalDepartment yyHospitalDepartment = yyHospitalDepartmentMapper.selectYyHospitalDepartmentByDepartmentId(Long.parseLong(String.valueOf(departmentId)));
        yyAppointment.setDepartment(yyHospitalDepartment.getDepartmentName());
        Long id = yyAppointmentTime.getId();
        yyAppointment.setTimeId(id.intValue());
        yyAppointmentMapper.insertYyAppointment(yyAppointment);
        yyAppointmentTime.setStatus("已约");
        yyAppointmentTimeMapper.updateYyAppointmentTime(yyAppointmentTime);
        return 1;
    }

    @Override
    public List<YyAppointment> selectAllYyAppointmentList(YyAppointment yyAppointment) {
        return yyAppointmentMapper.selectAllYyAppointmentList(yyAppointment);
    }
}
